package com.happycat.controller.social;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.happycat.base.PageParams;
import com.happycat.base.R;
import com.happycat.social.dto.SocialVoiceRoomMemberQueryDto;
import com.happycat.social.dto.SocialVoiceRoomMemberSaveDto;
import com.happycat.social.entity.SocialVoiceRoomMember;
import com.happycat.social.service.ISocialVoiceRoomMemberService;
import com.happycat.utils.ObjectsUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;

/**
 * @Author shenbin
 * @CreatedDate 2020-09-10
 * @Modified By:
 */
@RestController
@RequestMapping("/social/room/member")
@Api(tags = "API-聊天室-成员管理", value = "聊天室")
public class SocialRoomMemberController {
    @Autowired
    private ISocialVoiceRoomMemberService socialVoiceRoomMemberService;

    @ApiOperation(value = "新增名单", notes = "拉黑，禁言, 可操作权限: 房主、管理员, 管理员成员只能房主操作")
    @PostMapping(value = "/add")
    public R<SocialVoiceRoomMember> add(@RequestBody @Valid SocialVoiceRoomMemberSaveDto socialVoiceRoomMemberSaveDto){
        return R.success(socialVoiceRoomMemberService.add(socialVoiceRoomMemberSaveDto));
    }

    @ApiOperation(value = "批量删除名单", notes = "取消黑名单/取消禁言，可操作权限：房主、管理员")
    @PostMapping(value = "/del/{ids}")
    @ApiImplicitParam(name = "ids", value = "成员主键id，多个id可逗号分隔", required = true)
    public R<Boolean> del(@PathVariable @NotBlank String ids){
        ObjectsUtil.requireNonNull(ids, "成员不能为空");
        return R.success(socialVoiceRoomMemberService.del(ids));
    }

    @ApiOperation(value = "校验某个用户是否成员", notes = "校验某个用户是否黑名单/被禁言, 返回结果true/1为是，false/0否")
    @GetMapping(value = "/check")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "svrId", value = "房间平台主键id", required = true),
        @ApiImplicitParam(name = "type", value = "成员类型:1.黑名单, 2.被禁言名单, 3.管理员, 4.普通成员", required = true),
        @ApiImplicitParam(name = "accountId", value = "被检查用户id", required = true)
    })
    public R<Boolean> check(@RequestParam @NotNull Long svrId,
                            @RequestParam @NotNull @Range(min = 1, max = 4, message = "非法成员类型") Integer type,
                            @RequestParam @NotNull Long accountId){
        ObjectsUtil.requireNonNull(svrId, "房间不能为空");
        ObjectsUtil.requireNonNull(type, "成员类型不能为空");
        ObjectsUtil.requireNonNull(accountId, "被检查用户不能为空");

        return R.success(socialVoiceRoomMemberService.check(svrId, type, accountId));
    }

    @ApiOperation(value = "查询成员列表", notes = "黑名单/禁言名单列表")
    @PostMapping(value = "/pageList")
    public R<IPage<SocialVoiceRoomMember>> pageList(@RequestBody @Valid PageParams<SocialVoiceRoomMemberQueryDto> pageParams){
        IPage<SocialVoiceRoomMember> page = pageParams.buildPage();

        SocialVoiceRoomMember memberWs = BeanUtil.copyProperties(pageParams.getModel(), SocialVoiceRoomMember.class);

        return R.success(socialVoiceRoomMemberService.page(page, new QueryWrapper<>(memberWs)));
    }
}
